package cn.pigeon.health.service.impl;

import cn.pigeon.health.dao.PermissionDao;
import cn.pigeon.health.dao.RoleDao;
import cn.pigeon.health.dao.UserDao;
import cn.pigeon.health.pojo.Permission;
import cn.pigeon.health.pojo.Role;
import cn.pigeon.health.pojo.User;
import cn.pigeon.health.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * 用户服务层实现类
 */
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUserName(String userName) {
        //查找用户基本信息
        User user = userDao.findByUserName(userName);
        if(user==null){
            return null;
        }
        //查找用户对应的角色集合
        Set<Role> roles = roleDao.findByUserId(user.getId());
        if(roles!=null && roles.size()>0) {
            user.setRoles(roles);
            //查找角色对应的权限
            for (Role role : roles) {
                Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
                if(permissions!=null && permissions.size()>0) {
                    role.setPermissions(permissions);
                }
            }
        }
        return user;
    }
}
